using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using Microsoft.Xna.Framework;
using CocosDenshion;

namespace CT.HelloMonoCocos.Scenes
{
    public enum MoveDirection
    {
        Left,
        Top,
        Right,
        Bottom
    }

    public class HelloScene : CCLayerColor
    {
        private MoveDirection _currentDirection;
        private Random _randomizer = new Random();

        /// <summary>
        ///  Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
        /// </summary>
        public override bool init()
        {
            CCDirector.sharedDirector().deviceOrientation = ccDeviceOrientation.CCDeviceOrientationLandscapeLeft;

            if (!base.initWithColor(new ccColor4B(255, 255, 255, 255)))
            {
                return false;
            }

            CCSize size = CCDirector.sharedDirector().getWinSize();

            CCSprite logoSprite = CCSprite.spriteWithFile("ConceptTechnologies.jpg");
            logoSprite.position = new CCPoint(size.width / 2, size.height / 2);

            addChild(logoSprite);

            this.m_bIsTouchEnabled = true;

            //SimpleAudioEngine.sharedEngine().playBackgroundMusic("backgroundMusic.mp3", true);

            OnNextMove(logoSprite);

            return true;
        }

        public override void registerWithTouchDispatcher()
        {
            CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, 0, false);
        }

        public override void ccTouchEnded(CCTouch pTouch, CCEvent event_)
        {
            CCPoint point = pTouch.locationInView();

            CCSprite logoSprite = (CCSprite)children[0];

            if (CCRect.CCRectContainsPoint(logoSprite.boundingBox(), point))
            {
                //SimpleAudioEngine.sharedEngine().playEffect("tap.mp3");
                Windows.System.Launcher.LaunchUriAsync(new Uri("http://en.concepttechnologies.net"));
            }
        }

        private void OnNextMove(CCNode sender)
        {
            CCSprite logoSprite = (CCSprite)sender;

            CCSize size = CCDirector.sharedDirector().getWinSize();

            float leftBorder = logoSprite.textureRect.size.width / 2;
            float bottomBorder = logoSprite.textureRect.size.height / 2;
            float topBorder = size.height - (logoSprite.textureRect.size.height / 2);
            float rightBorder = size.width - (logoSprite.textureRect.size.width / 2);

            float anyX = _randomizer.Next((int)(logoSprite.textureRect.size.width / 2), (int)(size.width - logoSprite.textureRect.size.width / 2));
            float anyY = _randomizer.Next((int)(logoSprite.textureRect.size.height / 2), (int)(size.height - logoSprite.textureRect.size.height / 2));

            CCPoint movePoint = new CCPoint();

            switch (_currentDirection)
            {
                case MoveDirection.Left:
                    movePoint = new CCPoint(anyX, topBorder);
                    _currentDirection = MoveDirection.Top;
                    break;
                case MoveDirection.Top:
                    movePoint = new CCPoint(rightBorder, anyY);
                    _currentDirection = MoveDirection.Right;
                    break;
                case MoveDirection.Right:
                    movePoint = new CCPoint(anyX, bottomBorder);
                    _currentDirection = MoveDirection.Bottom;
                    break;
                case MoveDirection.Bottom:
                    movePoint = new CCPoint(leftBorder, anyY);
                    _currentDirection = MoveDirection.Left;
                    break;
            }

            var moveAction = CCMoveTo.actionWithDuration(1, movePoint);
            var loopCallback = CCCallFuncN.actionWithTarget(logoSprite, OnNextMove);

            var sequence = CCSequence.actionOneTwo(moveAction, loopCallback);

            logoSprite.runAction(sequence);
        }


        #region Scene static creation methods

        /// <summary>
        /// there's no 'id' in cpp, so we recommand to return the exactly class pointer
        /// </summary>
        public static CCScene scene()
        {
            // 'scene' is an autorelease object
            CCScene scene = CCScene.node();

            // 'layer' is an autorelease object
            CCLayer layer = HelloScene.node();

            // add layer as a child to scene
            scene.addChild(layer);

            // return the scene
            return scene;
        }

        public static new CCLayer node()
        {
            HelloScene ret = new HelloScene();
            if (ret.init())
            {
                return ret;
            }
            else
            {
                ret = null;
            }

            return ret;
        }

        #endregion

    }
}
